package com.ycs.operationlog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 如果配置文件没有在默认目录下，使用注解@PropertySource获取，下面演示的是在多配置文件中获取相同属性名的值，以后置为准
 * 单配置文件只要一个路径参数就可以
 *
 * @author ycs
 */
@Repository
public class InitTable {


    @Value("${operationLog.source.driverClassName}")
    private String driverClassName;

    @Value("${operationLog.source.url}")
    private String url;

    @Value("${operationLog.source.username}")
    private String username;

    @Value("${operationLog.source.password}")
    private String password;

    @Autowired
    private DataSourceConfig dataSourceConfig;


    @PostConstruct
    public void init() throws SQLException {
        DataSource dataSource = dataSourceConfig.getDataSource();

        Connection conn = dataSource.getConnection();

        Statement stat = conn.createStatement();
        //获取数据库表名
        ResultSet rs = conn.getMetaData().getTables(null, null, "sys_operate_log", null);

        // 判断表是否存在，如果存在则什么都不做，否则创建表
        if (rs.next()) {
            return;
        } else {
            stat.executeUpdate("CREATE TABLE `sys_operate_log` (\n" +
                    "  `oper_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '日志主键',\n" +
                    "  `title` varchar(50) DEFAULT '' COMMENT '模块标题',\n" +
                    "  `business_type` int(11) DEFAULT '0' COMMENT '业务类型（0其它 1新增 2修改 3删除）',\n" +
                    "  `method` varchar(255) DEFAULT '' COMMENT '方法名称',\n" +
                    "  `request_method` varchar(10) DEFAULT '' COMMENT '请求方式',\n" +
                    "  `operator_type` int(11) DEFAULT '0' COMMENT '操作类别（0其它 1后台用户 2手机端用户）',\n" +
                    "  `oper_name` varchar(50) DEFAULT '' COMMENT '操作人员',\n" +
                    "  `dept_name` varchar(50) DEFAULT '' COMMENT '部门名称',\n" +
                    "  `oper_url` varchar(255) DEFAULT '' COMMENT '请求URL',\n" +
                    "  `oper_ip` varchar(50) DEFAULT '' COMMENT '主机地址',\n" +
                    "  `oper_location` varchar(255) DEFAULT '' COMMENT '操作地点',\n" +
                    "  `oper_param` varchar(2000) DEFAULT '' COMMENT '请求参数',\n" +
                    "  `json_result` longtext COMMENT '返回参数',\n" +
                    "  `status` int(11) DEFAULT '0' COMMENT '操作状态（0正常 1异常）',\n" +
                    "  `error_msg` varchar(2000) DEFAULT '' COMMENT '错误消息',\n" +
                    "  `oper_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '操作时间',\n" +
                    "  PRIMARY KEY (`oper_id`)\n" +
                    ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='操作日志记录';"
            );
        }
        // 释放资源
        stat.close();
        conn.close();
    }
}
